/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* FreqDist.java
*
* Created on January 4, 2008, 4:50 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package javautil.stat;

import javautil.collections.ListUtil;

import java.util.Hashtable;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author stewari1
 */
public class FreqDist {

    /** Field description */
    private final Map<Object, Integer> freqDistMap = new Hashtable<>();

    /** Field description */
    private List list = null;

    /**
     * Constructs ...
     *
     * @param list description
     */
    public FreqDist(List list) {

        this.list = list;

        Set set = new LinkedHashSet(list);

        for (Object obj : set) {

            freqDistMap.put(obj, new Integer(ListUtil.getFreq(list, obj)));
        }
    }

    /**
     * map of objects and their occurences (indices) in the list.
     */
    public Map<Object, List<Integer>> getDataPositions(int freqThreshold) {

        Map<Object, List<Integer>> dataPosMap = new Hashtable<>();
        Set keySet                            = freqDistMap.keySet();

        for (Object obj : keySet) {

            if (freqDistMap.get(obj).intValue() > freqThreshold)
                dataPosMap.put(obj, ListUtil.getIndexList(list, obj));
        }

        return dataPosMap;
    }

    public Map<Object, Integer> getFreqDistMap() {
        return freqDistMap;
    }
}
